package com.enjoyor.health.HGPT;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author Zhu Min
 * @date 2023/10/18 11:30
 */
public class CodeGenerator {

    public static void main(String... args){

        FastAutoGenerator.create("jdbc:mysql://192.168.93.41:3306/health_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC",
                        "health_user",
                        "hIQ1/4JLYwqa2mYfFA0xyr.jFas")
                .globalConfig(builder -> {
                    builder.author("enjoyor") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D://tmp//health"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.enjoyor.health.HGPT") // 设置父包名
                            .moduleName("core") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "D://tmp//health")); // 设置mapperXml生成路径
                })
                /*.strategyConfig(builder -> {
                    builder.addInclude("user_info") // 设置需要生成的表名
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })*/
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }

}
